from google.appengine.ext import db

class CoolCategory(db.Model):
    owner = db.UserProperty()
    name = db.StringProperty()
    bookmarkcount = db.IntegerProperty()
    description = db.StringProperty(multiline=True)
    createtime = db.DateTimeProperty(auto_now_add=True)
    lastmodifiedtime = db.DateTimeProperty(auto_now=True)

    @property
    def id(self):
        return str(self.key().id())

class CoolTag(db.Model):
    name = db.StringProperty()
    tagindex = db.IntegerProperty()
    bookmarkcount = db.IntegerProperty()
    description = db.StringProperty(multiline=True)
    createtime = db.DateTimeProperty(auto_now_add=True)
    lastmodifiedtime = db.DateTimeProperty(auto_now=True)

    @property
    def id(self):
        return str(self.key().id())

class CoolBookmark(db.Model):
    owner = db.UserProperty()
    category = db.ReferenceProperty(CoolCategory)
    tags = db.StringListProperty()
    name = db.StringProperty()
    url = db.LinkProperty()
    description = db.StringProperty(multiline=True)
    accesscount = db.IntegerProperty()
    comments = db.IntegerProperty()
    privilege = db.StringProperty(default="private")
    createtime = db.DateTimeProperty(auto_now_add=True)
    lastmodifiedtime = db.DateTimeProperty(auto_now=True)
    image = db.BlobProperty()
    zipcontent = db.BlobProperty()
    #starrate min 0, max 100
    totalvote = db.IntegerProperty(default=0)
    totalvotecount = db.IntegerProperty(default=0)

    @property
    def id(self):
        return str(self.key().id())
    
    @property
    def starrate(self):
        result = 0
        if self.totalvote == 0:
            result = 0
        else:
            if self.totalvotecount == 0:
                result = 0
            else:
                result = (float(self.totalvote) / float(self.totalvotecount * 5))*100
        return result

class CoolComment(db.Model):
    owner = db.UserProperty()
    bookmark = db.ReferenceProperty(CoolBookmark)
    content = db.StringProperty(multiline=True)    
    createtime = db.DateTimeProperty(auto_now_add=True)
    lastmodifiedtime = db.DateTimeProperty(auto_now=True)

    @property
    def id(self):
        return str(self.key().id())

class Devlog(db.Model):
    content = db.StringProperty(multiline=True)    
    createtime = db.DateTimeProperty(auto_now_add=True)

    @property
    def id(self):
        return str(self.key().id())
